package cn.cyber.wxfire.controller;

import cn.cyber.wxfire.pojo.News;
import cn.cyber.wxfire.pojo.Student;
import cn.cyber.wxfire.pojo.Teacher;
import cn.cyber.wxfire.service.SendSessionService;
import cn.cyber.wxfire.service.StudentServer;
import cn.cyber.wxfire.service.TeacherServer;
import cn.cyber.wxfire.vo.studentCourseVo;
import cn.cyber.wxfire.vo.teacherCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Controller
public class TeacherController {

    @Autowired
    private StudentServer studentServer;

    @Autowired
    private TeacherServer teacherServer;

    private SendSessionService sendSessionService=new SendSessionService();

    public List<Student> StudentSort(List<Student> sl, int flag){
        if(flag==0){
            Collections.sort(sl, new Comparator<Student>() {
                @Override
                public int compare(Student p1, Student p2) {
                    if(Integer.parseInt(p1.getNum())>Integer.parseInt(p2.getNum()))
                    {
                        return -1;
                    }
                    else{
                        return 1;
                    }
                }
            });
        }
        else
        {
            Collections.sort(sl, new Comparator<Student>() {
                @Override
                public int compare(Student p1, Student p2) {
                    if(Integer.parseInt(p1.getNum())>Integer.parseInt(p2.getNum()))
                    {
                        return 1;
                    }
                    else{
                        return -1;
                    }
                }
            });
        }
        return sl;
    }



    @RequestMapping("/teacher/all/{num}/{flag}")
    public String queryStudentList(@PathVariable("flag") int flag,@PathVariable("num") String num,Model model,HttpSession session)
    {
        List<Student> studentsList=studentServer.studentCourseList(num);
        studentsList=StudentSort(studentsList,flag);
        model.addAttribute("stus",studentsList);
        model.addAttribute("num",num);
        session.setAttribute("cnum",num);
        sendSessionService.sendSession(model, session);

        return "teacher/list";
    }

    @GetMapping("/teacher/privateUpdate/{num}")
    public String privateUpdate(@PathVariable("num") String num,Model model,HttpSession session)
    {
        List<Teacher> teachersList=teacherServer.queryTeacherList();
        for (Teacher tea:teachersList) {
            if(tea.getNum().equals(num))
            {

                model.addAttribute("stu_u",tea);
                sendSessionService.sendSession(model, session);
                break;
            }

        }
        return "/teacher/privateUpdate";
    }

    @RequestMapping(value = "/teacher/privateUpdate/finish/{num}",method = RequestMethod.POST)
    public String privateUpdateStudent(Teacher teacher,
                                       HttpServletResponse response,
                                       @PathVariable("num") String num,
                                       @RequestParam("OldPassword") String OldPassword,
                                       @RequestParam("NewPassword") String NewPassword,
                                       @RequestParam("SurePassword") String SurePassword,
                                       Model model, HttpSession session)
    {

        response.setCharacterEncoding("GBK");
        List<Student> studentsList=studentServer.queryStudentList();
        List<Teacher> teachersList=teacherServer.queryTeacherList();
        String password="";
        for (Teacher tea:teachersList) {
            if(tea.getNum().equals(num))
            {
                password=tea.getPassword();
                break;
            }
        }
        if(password.equals(OldPassword)){
            if(NewPassword.equals(SurePassword)){
                teacher.setNum(num);
                teacher.setPassword(NewPassword);
                teacherServer.privateUpdateTeacher(teacher);
                studentsList=studentServer.queryStudentList();
                studentsList=StudentSort(studentsList,1);
                model.addAttribute("stus",studentsList);
                sendSessionService.sendSession(model, session);
                return "teacher/list";
            }
            else{

//                model.addAttribute("msg2","新密码与确认密码不一样");
                PrintWriter out = null;
                try {
                    out = response.getWriter();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out.print("<script>alert('新密码与确认密码不一样'); window.location.href='http://localhost:80/teacher/privateUpdate/"+num+"' </script>");
                out.flush();
                out.close();
                sendSessionService.sendSession(model, session);
                return "redirect:../teacher/privateUpdate/"+num;
            }
        }
        else{
//            model.addAttribute("msg2","原密码不正确");

            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.print("<script>alert('原密码不正确'); window.location.href='http://localhost:80/teacher/privateUpdate/"+num+"' </script>");
            out.flush();
            out.close();
            sendSessionService.sendSession(model, session);
            return "redirect:../teacher/privateUpdate/"+num;
        }



    }

    @RequestMapping("/courses")
    public String StudentCourseList( Model model,HttpSession session){
        List<Student> studentsList=studentServer.queryStudentList();
        List<Teacher> teachersList=teacherServer.queryTeacherList();

        //获取身份和账号
        String num=(String) session.getAttribute("loginUser");
        String identity =(String) session.getAttribute("identity");

        if(identity.equals("student")){
            //身份为学生

            //向前端传该账号选课信息
            List<studentCourseVo> studentCourseVos=studentServer.StudentCourse(num);
            model.addAttribute("CourseVos",studentCourseVos);
            System.out.println(studentCourseVos);


        }
        else {
            //向前端传该账号授课信息
            List<teacherCourseVo> teacherCourseVos = teacherServer.teacherCourse(num);
            model.addAttribute("CourseVos", teacherCourseVos);
            System.out.println(teacherCourseVos);


        }
        sendSessionService.sendSession(model, session);
            return "commons/classTotal";
        }

    @GetMapping("/personCenter")
    private String personCenter( Model model,HttpSession session){
        List<Student> studentsList=studentServer.queryStudentList();
        List<Teacher> teachersList=teacherServer.queryTeacherList();

        //获取身份和账号
        String num=(String) session.getAttribute("loginUser");
        String identity =(String) session.getAttribute("identity");

        if(identity.equals("student")){
            //身份为学生

            //向前端传该账号选课信息
            List<studentCourseVo> studentCourseVos=studentServer.StudentCourse(num);
            model.addAttribute("CourseVos",studentCourseVos);
            System.out.println(studentCourseVos);

            //向前端传该账号基础信息
            for (Student stu:studentsList) {
                if(num.equals(stu.getNum())){
                    model.addAttribute("per",stu);
                }
            }
        }
        else{
            //向前端传该账号授课信息
            List<teacherCourseVo> teacherCourseVos=teacherServer.teacherCourse(num);
            model.addAttribute("CourseVos",teacherCourseVos);
            System.out.println(teacherCourseVos);

            for (Teacher teacher:teachersList) {
                if(num.equals(teacher.getNum())){
                    model.addAttribute("per",teacher);
                }
            }
        }








        List<News> newsList=teacherServer.queryNewsList();
        model.addAttribute("newsList",newsList);
        sendSessionService.sendSession(model, session);
        return "/commons/personCenter";
    }



}
